<template>
  <el-dialog
    :close-on-click-modal="false"
    width="45%"
    :title="title"
    :visible.sync="visible"
    :before-close="close"
    lock-scroll
    append-to-body
  >
    <el-form status-icon :rules="rules" :model="datas" @keyup.enter.native="save" ref="editForm">
      <el-row :gutter="10">
        <el-col :span="23">
          <el-form-item prop="name" label="分类名称" :label-width="baseStyle.width">
            <el-input v-model="datas.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="23">
          <el-form-item prop="weight" label="权重" :label-width="baseStyle.width">
            <el-input-number
              v-model="datas.weight"
              :precision="0"
              :step="1"
              :min="0"
              :max="100"
              style="width:100%"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="23">
          <el-form-item prop="sort" label="排序" :label-width="baseStyle.width">
            <el-input-number
              v-model="datas.sort"
              :precision="0"
              :min="0"
              :step="1"
              style="width:100%"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button :type="'primary'" :loading="loading" @click.native="save()">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { Message } from "element-ui";

export default {
  name: "EditForm",
  props: {
    visible: Boolean,
    baseStyle: Object,
    title: String,
    datas: {
      type: Object,
      default: function () {
        return { weight: "" };
      },
    },
    saveForm: Function,
  },
  data() {
    const rules = this.getRules();
    return {
      loading: false,
      rules,
    };
  },
  computed: {},
  created() {},
  methods: {
    cancel() {
      this.loading = false;
      this.$emit("update:visible", false);
      this.$refs.editForm.resetFields();
    },
    close() {
      this.cancel();
    },
    save() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$emit("saveForm", { ...this.datas });
        } else {
          return false;
        }
      });
    },
    getRules() {
      return {
        name: [
          {
            validator: (rule, value, callback) => {
              if (!this.datas.name) {
                callback(new Error(`请填写类别名称`));
                return;
              }
              if (this.datas.name.trim().length < 1) {
                callback(new Error(`请填写类别名称`));
                return;
              }
              callback();
            },
            trigger: "blur",
          },
        ],
        weight: [
          {
            validator: (rule, value, callback) => {
              if (this.datas.weight < 0) {
                callback(new Error(`请填写权重（最小值为0）`));
                return;
              }
              callback();
            },
            trigger: "blur",
          },
        ],
      };
    },
  },
};
</script>
<style lang="scss" scoped>
</style>